package com.gdziarmaga.webshop.author.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.gdziarmaga.webshop.book.model.Book;

@Entity
@Table(name = "Author")
public class Author implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String AUTHOR_FIRST_NAME = "author_first_name";
	public static final String AUTHOR_LAST_NAME = "author_last_name";

	private long author_id;

	private String author_first_name;
	private String author_last_name;

	private List<Book> book_list;

	// private Set<Book> book_set;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "author_seq")
	@SequenceGenerator(name = "author_seq", sequenceName = "AUTHOR_SEQ")
	@Column(name = "author_id", precision = 20, scale = 0, unique = true)
	public long getAuthor_id() {
		return author_id;
	}

	public void setAuthor_id(long author_id) {
		this.author_id = author_id;
	}

	@Column(name = "author_first_name", nullable = false, length = 100)
	public String getAuthor_first_name() {
		return author_first_name;
	}

	public void setAuthor_first_name(String author_first_name) {
		this.author_first_name = author_first_name;
	}

	@Column(name = "author_last_name", nullable = false, length = 100)
	public String getAuthor_last_name() {
		return author_last_name;
	}

	public void setAuthor_last_name(String author_last_name) {
		this.author_last_name = author_last_name;
	}

	//
	// public void setBook_set(Set<Book> book_set) {
	// this.book_set = book_set;
	// }

	// @ManyToMany(cascade = CascadeType.ALL)
	// @JoinTable(name = "Book_Author", joinColumns = {
	// @JoinColumn(name = "author_id", nullable = false, updatable = false) },
	// inverseJoinColumns = { @JoinColumn(name = "book_id",
	// nullable = false, updatable = false) })
	// @ManyToMany(mappedBy = "author_list", fetch = FetchType.EAGER)
	// public Set<Book> getBook_set() {
	// return book_set;
	// }

	public void setBook_list(List<Book> book_list) {
		this.book_list = book_list;
	}

	// @ManyToMany(cascade = CascadeType.ALL)
	// @JoinTable(name = "Book_Author", joinColumns = {
	// @JoinColumn(name = "author_id", nullable = false, updatable = false) },
	// inverseJoinColumns = { @JoinColumn(name = "book_id",
	// nullable = false, updatable = false) })
	@ManyToMany(mappedBy = "author_set", fetch = FetchType.EAGER)
	public List<Book> getBook_list() {
		return book_list;
	}

	@Override
	public String toString() {
		return author_first_name + " " + author_last_name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((author_first_name == null) ? 0 : author_first_name
						.hashCode());
		result = prime
				* result
				+ ((author_last_name == null) ? 0 : author_last_name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Author other = (Author) obj;
		if (author_first_name == null) {
			if (other.author_first_name != null)
				return false;
		} else if (!author_first_name.equals(other.author_first_name))
			return false;
		if (author_last_name == null) {
			if (other.author_last_name != null)
				return false;
		} else if (!author_last_name.equals(other.author_last_name))
			return false;
		return true;
	}
}
